package code

import (
	"fmt"
)

var (
	OK         = &Err{Code: "00000", Message: "一切Ok"}
	ERROR      = &Err{Code: "B0001", Message: "系统执行出错"}
	USER_ERROR = &Err{"A0001", "用户端错误"}
)

// Err represents an error
type Err struct {
	Code    string
	Message string
}

func (err *Err) Error() string {
	return fmt.Sprintf("Err - code: %d, message: %s, error: %s", err.Code, err.Message, "")
}

// DecodeErr ...
func DecodeErr(err any) *Err {
	if err == nil {
		return OK
	}
	switch typed := err.(type) {
	case *Err:
		return typed
	default:
		return ERROR
	}
}
func NewErr(err string) *Err {
	return &Err{
		Code:    ERROR.Code,
		Message: err,
	}
}
